const app = getApp();
const my = require('../../classes/lib/util.js');
const user = require('../../classes/application/user.js');
const address = require('../../classes/application/address.js');
const application = require('../../classes/application/application.js');
Page({

  data: {
    themes: null,
    user: null,
    formValues: null,
    verify_code: null,
    sendVerifyCodeText: '发送验证码',
    nextTimes: 0,
    mobile: '',
  },
  onLoad: function (options) {
    this.setData({
      themes: app.globalData.themes,
      user: app.globalData.userInfo,
      mobile : app.globalData.userInfo.mobile,
    })
  },
  freshTime: function () {
    if (this.data.nextTimes == 0) {
      this.setData({
        sendVerifyCodeText: '发送验证码',
      });
    } else {
      this.setData({
        sendVerifyCodeText: this.data.nextTimes + 'S',
        nextTimes: this.data.nextTimes - 1
      });
      setTimeout(this.freshTime, 1000);
    }
  },
  mobileInput: function (e) {
    this.setData({
      mobile: e.detail.value
    });
  },
  formSubmit: function (e) {
    var data = e.detail.value;
    this.setData({
      formValues: data
    });
    if (this.data.mobile == '') {
      my.toast("请输入手机号码");
      return;
    } else if (data.verify_code == '') {
      my.toast("请输入验证码");
      return;
    } else if (data.verify_code != this.data.verify_code) {
      my.toast("验证码错误");
      return;
    }
    console.log(JSON.stringify(this.data.formValues));
    this.bind();
  },
  sendVerifyCode: function (e) {
    console.log("password:sendVerifyCode:invoke");
    if (!this.data.mobile || this.data.mobile == '') {
      my.toast("请输入手机号码");
      return;
    }
    this.setData({
      nextTimes: 120,
      verify_code: my.random(1001, 9999),
    });
    this.freshTime();
    application.send_verfiy_code({
      uid: app.globalData.game.uid,
      mobile: this.data.mobile,
      code: this.data.verify_code,
      callback: function (data) {

      }
    })
  },

  bind: function (e) {
    user.bind({
      uid: app.globalData.game.uid,
      openid: app.globalData.openid,
      mobile: this.data.mobile,
      password: this.data.formValues.password,
      callback: function (data) {
        my.toast("绑定c")
        wx.navigateBack({
          delta: -1,
        });
      }
    });
  },
})